import BaseController from '../../core/controllers/Base.controller';

@Inject('$log', '$state', 'deviceService', 'ngDialog', 'ngDialogService')
class WorkerInfoDetailController extends BaseController {
  constructor() {
    super();

    this.equipId = $state.params.id;
    this.selectedIndex = 0;
    this.currentIndex = 0;
    this.$tableCheck = {
      fetchTimes: 0,
      isTableLoading: false,
      pagination: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.$tableMaintain = {
      fetchTimes: 0,
      isTableLoading: false,
      pagination: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.$tableInOut = {
      fetchTimes: 0,
      isTableLoading: false,
      pagination: {
        ITEMS_PER_PAGE: 10,
        MAX_SIZE: 5,
        currentPage: 1,
        total: 0,
      },
    };
    this.init();
    this.$paginationQuery(this.equipId);
  }

  $paginationQuery(id) {
    this.$getDeviceInfoDetail(id);
    this.$getDeviceCheck(id);
    this.$getDeviceMaintain(id);
    this.$getDeviceInOut(id);
  }

  // 获取设备详情
  $getDeviceInfoDetail(id) {
    this.deviceService
      .get(id)
      .then((res) => {
        this.deviceDetailInfo = res.data;
      });
  }

  // 获得头像src
  /*eslint-disable */
  $getAvatarSrc() {
    if (this.deviceDetailInfo && this.deviceDetailInfo.image &&
      this.deviceDetailInfo.image.filePath) {
      const src = `/${this.deviceDetailInfo.image.filePath}`;
      return src;
    } else {
      return require('../../assets/images/' + 'addBt.jpg');
    }
  }

  // 获取检测记录
  $getDeviceCheck(id) {
    this.$tableCheck.isTableLoading = true;
    this.deviceService
      .queryCheck({
        mapParams: { deviceId: id },
        page: this.$tableCheck.pagination.currentPage,
        size: this.$tableCheck.pagination.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.deviceCheckInfo = res.rows;
        this.$tableCheck.pagination.totalItems = res.total;
        this.$tableCheck.pagination.total = res.total;
        this.$tableCheck.isTableLoading = false;
        if (res.total < 1) {
          this.$tableCheck.fetchTimes += 1;
        }
      });
  }

  // 获取维保记录
  $getDeviceMaintain(id) {
    this.$tableMaintain.isTableLoading = true;
    this.deviceService
      .queryMaintain({
        mapParams: { deviceId: id },
        page: this.$tableMaintain.pagination.currentPage,
        size: this.$tableMaintain.pagination.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.$log.debug(res);
        this.deviceMaintainInfo = res.rows;
        this.$tableMaintain.pagination.totalItems = res.total;
        this.$tableMaintain.pagination.total = res.total;
        this.$tableMaintain.isTableLoading = false;
        if (res.total < 1) {
          this.$tableMaintain.fetchTimes += 1;
        }
      });
  }

  // 获取设备进出场记录
  $getDeviceInOut(id) {
    this.$tableInOut.isTableLoading = true;
    this.deviceService
      .queryInOut({
        mapParams: { deviceId: id },
        page: this.$tableInOut.pagination.currentPage,
        size: this.$tableInOut.pagination.ITEMS_PER_PAGE,
      })
      .then((res) => {
        this.deviceInOutInfo = res.rows;
        this.$tableInOut.pagination.totalItems = res.total;
        this.$tableInOut.isTableLoading = false;
        if (res.total < 1) {
          this.$tableInOut.fetchTimes += 1;
        }
      });
  }

  $addCheckDialog(ctrl) {
    this.ngDialog.open({
      template: 'paAddCheckDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, $state, ngDialogService, deviceService) {
        'ngInject';

        this.reqData = {
          deviceId: $state.params.id,
          checkTime: '',
          nextCheckTime: '',
          checkResult: '',
          attachmentIds: [],
        };

        this.view = {
          attachmentFiles: [],
        };

        this.submitForm = (param) => {
          if (param.checkTime.$viewValue > param.nextCheckTime.$viewValue ||
            param.checkTime.$viewValue === param.nextCheckTime.$viewValue) {
            ngDialogService.alertError('下次检测时间应该大于检测时间');
            return;
          }
          this.reqData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          deviceService.saveCheck(this.reqData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess('新增成功');
                $scope.closeThisDialog();
                ctrl.$getDeviceCheck(ctrl.equipId);
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $editCheckDialog(ctrl, params) {
    this.ngDialog.open({
      template: 'paEditCheckDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($scope, ngDialogService, deviceService) {
        'ngInject';

        deviceService.getCheck({ id: params.id })
          .then((res) => {
            if (res.success) {
              this.checkData = res.data;
              this.view = {
                attachmentFiles: this.checkData.files,
              };
              this.view.attachmentFiles.forEach((item) => {
                item.name = item.fileName;
              });
            } else {
              ngDialogService.alertError(res.message);
            }
          });

        this.submitEditForm = (param) => {
          if (param.checkTime.$viewValue > param.nextCheckTime.$viewValue ||
            param.checkTime.$viewValue === param.nextCheckTime.$viewValue) {
            ngDialogService.alertError('下次检测时间应该大于检测时间');
            return;
          }
          if (this.view.attachmentFiles) {
            this.checkData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          }
          deviceService.updateCheck(this.checkData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess('修改成功');
                $scope.closeThisDialog();
                ctrl.$getDeviceCheck(ctrl.equipId);
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $deleteCheck(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.deviceService
          .deleteCheck([id])
          .then((res) => {
            this.$log.debug(res);
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$getDeviceCheck(this.equipId);
            }
          });
      });
  }

  $addMaintainDialog(ctrl) {
    this.ngDialog.open({
      template: 'paAddMaintainDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller($rootScope, $scope, $state, ngDialogService, deviceService) {
        'ngInject';

        this.view = {
          attachmentFiles: [],
        };

        this.reqData = {
          deviceId: $state.params.id,
          maintainTime: '',
          nextMaintainTime: '',
          maintainContent: '',
          attachmentIds: [],
        };

        this.submitForm = (param) => {
          if (param.maintainTime.$viewValue > param.nextMaintainTime.$viewValue ||
            param.maintainTime.$viewValue === param.nextMaintainTime.$viewValue) {
            ngDialogService.alertError('下次维保时间应该大于维保时间');
            return;
          }
          this.reqData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          deviceService.saveMaintain(this.reqData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess('新增成功');
                $scope.closeThisDialog();
                ctrl.$getDeviceMaintain(ctrl.equipId);
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $editMaintainDialog(ctrl, params) {
    this.ngDialog.open({
      template: 'paEditMaintainDialog',
      className: 'ngdialog-theme-default pa-confirm-theme pa-confirm-self systemManage-add-dialog',
      controllerAs: 'vm',
      controller: function ctr($scope, ngDialogService, deviceService) {
        'ngInject';

        deviceService.getMaintain({ id: params.id })
          .then((res) => {
            if (res.success) {
              this.maintainData = res.data;
              this.view = {
                attachmentFiles: this.maintainData.files,
              };
              this.view.attachmentFiles.forEach((item) => {
                item.name = item.fileName;
              });
            } else {
              ngDialogService.alertError(res.message);
            }
          });

        this.submitEditForm = (param) => {
          if (param.maintainTime.$viewValue > param.nextMaintainTime.$viewValue ||
            param.maintainTime.$viewValue === param.nextMaintainTime.$viewValue) {
            ngDialogService.alertError('下次维保时间应该大于维保时间');
            return;
          }
          if (this.view.attachmentFiles) {
            this.maintainData.attachmentIds = this.view.attachmentFiles.map(item => item.id);
          }
          deviceService.updateMaintain(this.maintainData)
            .then((res) => {
              if (res.success) {
                ngDialogService.alertSuccess('修改成功');
                $scope.closeThisDialog();
                ctrl.$getDeviceMaintain(ctrl.equipId);
              } else {
                ngDialogService.alertError(res.message);
              }
            });
        };
      },
    });
  }

  $deleteMaintain(id) {
    this.ngDialogService.confirm('确定删除此数据吗？', id)
      .then((data) => {
        this.$log.debug(data);
        this.deviceService
          .deleteMaintain([id])
          .then((res) => {
            if (res.success) {
              this.ngDialogService.alertSuccess(res.message);
              this.$getDeviceMaintain(this.deviceId);
            }
          });
      });
  }

  switchTab(item, $index) {
    this.selectedIndex = $index;
    this.name = item.name;
  }

  init() {
    this.name = '基本信息';
    this.tabData = [
      {
        id: '0',
        name: '基本信息',
      },
      {
        id: '1',
        name: '检验检测记录',
      },
      {
        id: '2',
        name: '维修保养记录',
      },
      {
        id: '3',
        name: '进出场记录',
      },
    ];
  }
}

export default WorkerInfoDetailController;
